<html>
<head><meta charset="utf-8"><title>Attaching backtraces to RefCell · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html">Attaching backtraces to RefCell</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226273614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226273614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226273614">(Feb 13 2021 at 22:57)</a>:</h4>
<p>Currently, it can be fairly difficult to debug a <code>Borrow</code>/<code>BorrowMut</code> panic from a <code>RefCell</code>. In a larger codebase, there may be lots of places where <code>.borrow()</code> and <code>.borrow_mut()</code> are called, so just seeing one backtrace may not be enough.</p>
<p>There are crates like <code>debug-cell</code> and <code>accountable-refcell</code>, which are <em>mostly</em> drop-in replacements for <code>RefCell</code> that track the backtraces of outstanding borrows. However, any such crate needs to provide its own <code>Ref</code> and <code>RefMut</code> types. If a crate uses any of the <code>Ref</code>/<code>RefMut</code> API functions explicitly (e.g <code>std::cell::RefMut::map(my_ref, |a| b)</code>), then it can be quite difficult to use one of those crates for debugging purposes.</p>
<p>Now that we have <code>Backtrace</code> in <code>libstd</code> and <code>cargo -Z build-std</code>, I think there's a possible solution. We can add an off-by-default feature <code>debug-refcell</code> to <code>libstd</code>. When enabled, it enables backtrace capturing for outstanding <code>Ref</code>/<code>RefMut</code> borrows, which get included in the panic message. When disabled, all of the extra fields and implementation get compiled out, so there's no additional overhead by default.</p>
<p>Actually using this feature will require using <code>cargo -Z build-std</code>. If it proves to be useful, we could try adding a nicer interface for users to enable it when debugging.</p>
<p>AFAIK, there's no precedent for this kind of feature in libstd. However, I think it could greatly improve the debugging experience without impacting normal users.</p>



<a name="226273690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226273690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226273690">(Feb 13 2021 at 22:58)</a>:</h4>
<p>This would be a very heavy hammer (backtraces will get captured for every crate in the dependency graph that uses <code>RefCell</code>, regardless if they ever panic). However, it will be off by default, so users can just choose not to use it if it's not feasible for a particular project</p>



<a name="226273771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226273771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226273771">(Feb 13 2021 at 23:00)</a>:</h4>
<p>I like this :) would you also turn it on when <code>debug = true</code> is set in config.toml?</p>



<a name="226273846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226273846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226273846">(Feb 13 2021 at 23:02)</a>:</h4>
<p>uh, maybe</p>



<a name="226273847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226273847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226273847">(Feb 13 2021 at 23:02)</a>:</h4>
<p>I think that could be decided later</p>



<a name="226282045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226282045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226282045">(Feb 14 2021 at 02:50)</a>:</h4>
<p>This feels like a T-libs question to me, personally, as it largely is about public API here (the implementation does not seem particularly challenging in an obvious way)</p>



<a name="226282521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226282521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226282521">(Feb 14 2021 at 03:04)</a>:</h4>
<p>I wasn't sure if cargo features for libstd actually counted as public API</p>



<a name="226282532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226282532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226282532">(Feb 14 2021 at 03:05)</a>:</h4>
<p>since the libstd crate itself requires nightly to build, and<code>cargo -Z build-std</code> is unstable</p>



<a name="226282592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226282592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226282592">(Feb 14 2021 at 03:07)</a>:</h4>
<p>Sure, but libs team currently evaluates unstable std additions</p>



<a name="226293544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226293544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226293544">(Feb 14 2021 at 08:43)</a>:</h4>
<p><code>Backtrace</code> would need to be part of libcore, right? That is where <code>RefCell</code> lives.</p>



<a name="226313176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226313176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226313176">(Feb 14 2021 at 16:31)</a>:</h4>
<p>Right - there's an open PR for that, I think</p>



<a name="226326667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226326667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226326667">(Feb 14 2021 at 21:51)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/77384">https://github.com/rust-lang/rust/pull/77384</a></p>



<a name="226332208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226332208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226332208">(Feb 14 2021 at 23:35)</a>:</h4>
<p>Just one bit of info: I've seen in the past (which might no longer be true) backtrace being <strong>super</strong> slow when called. If every borrow can potentially invoke it, performance _could_ suffer dramatically.</p>



<a name="226332261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226332261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226332261">(Feb 14 2021 at 23:36)</a>:</h4>
<p>Please have some benchmark ready to measure before merging any PR</p>



<a name="226335435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226335435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226335435">(Feb 15 2021 at 00:50)</a>:</h4>
<p>Well, it will be off by default</p>



<a name="226335448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226335448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226335448">(Feb 15 2021 at 00:51)</a>:</h4>
<p>So it doesn't really have to be optimized when it's on - it just needs to be useable</p>



<a name="226336220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226336220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226336220">(Feb 15 2021 at 01:11)</a>:</h4>
<p>Capturing the addresses of the stack should be feasible and fast; I don't think it's a public API yet but we can use it if desired in std. Resolving those can be done just on failure. In practice though I'd start with track caller and storing a finite amount of currently held borrows - I imagine it would be enough in 99% of cases. But maybe more work.</p>



<a name="226552731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226552731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226552731">(Feb 16 2021 at 18:12)</a>:</h4>
<p>IIRC, the issue was that symbol resolution was single-threaded or something like that, so just storing the addresses might indeed be fast enough for the critical path.</p>



<a name="226873173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Attaching%20backtraces%20to%20RefCell/near/226873173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Attaching.20backtraces.20to.20RefCell.html#226873173">(Feb 18 2021 at 20:15)</a>:</h4>
<p>Opened <a href="https://github.com/rust-lang/rust/pull/82271">https://github.com/rust-lang/rust/pull/82271</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>